package com.zhsq.zlc.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.io.File;
import java.io.FileInputStream;

public class ImageUtil {
    /**
     * 图片缩放
     * @param src 为源文件目录
     * @param dest 为缩放后保存目录
     * @param w，h为缩放的目标宽度和高度
     * @return
     */
    public static void zoomImage(String src,String dest,int w,int h) throws Exception {

        double wr=0,hr=0;
        File srcFile = new File(src);
        File destFile = new File(dest);

        BufferedImage bufImg = ImageIO.read(srcFile); //读取图片
        Image Itemp = bufImg.getScaledInstance(w, h, bufImg.SCALE_SMOOTH);//设置缩放目标图片模板

        wr=w*1.0/bufImg.getWidth();     //获取缩放比例
        hr=h*1.0 / bufImg.getHeight();

        AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null);
        Itemp = ato.filter(bufImg, null);
        try {
            ImageIO.write((BufferedImage) Itemp,dest.substring(dest.lastIndexOf(".")+1), destFile); //写入缩减后的图片
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 图片分割
     * @param src 为源文件目录
     * @param dest 为切分后保存目录
     * @param rows，cols为切分的行和列
     * @return
     */
    public static void cutImage(String src,String dest,int rows,int cols)throws Exception {
        File file = new File(src);
        FileInputStream fis = new FileInputStream(file);
        BufferedImage image = ImageIO.read(fis);
        int chunks = rows * cols;
        int chunkWidth = image.getWidth() / cols;
        int chunkHeight = image.getHeight() / rows;
        int count = 0;
        BufferedImage[] imgs = new BufferedImage[chunks];
        for (int x = 0; x < rows; x++) {
            for (int y = 0; y < cols; y++) {
                imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType());

                Graphics2D gr = imgs[count++].createGraphics();
                gr.drawImage(image, 0, 0, chunkWidth, chunkHeight,
                        chunkWidth * y, chunkHeight * x,
                        chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null);
                gr.dispose();
            }
        }
        for (int i = 1; i <= imgs.length; i++) {
            ImageIO.write(imgs[i-1], "png", new File(dest+ i + ".png"));
        }
    }
    /**
     * 4/5/6级图片分割
     * @param src 为源文件目录
     * @param dest 为切分后保存目录
     * @return
     */
    public static void cut456LevelImage(String src,String dest)throws Exception {
        String[] levelsImages={"temp/768-512","temp/1536-1024","temp/3072-2048"};
        for (int i=1;i<4;i++){
            if(!new File(dest+levelsImages[i-1]).exists()){
                new File(dest+levelsImages[i-1]).mkdir();
            }
            cutImage(dest+levelsImages[i-1]+".png",dest+levelsImages[i-1]+"/",2*((int)Math.pow(2,i-1)),3*((int)Math.pow(2,i-1)));
        }
    }

    /**
     * 4/5/6级图片分目录保存
     * @param src 为源文件目录
     * @param dest 为分目录后保存目录
     * @return
     */
    public static void save456LevelImage(String src,String dest)throws Exception {
        FileUtil fu=new FileUtil();
        String[] levelsImages={"temp/768-512/","temp/1536-1024/","temp/3072-2048/"};
        int[] levelsImagesCount={6,};
        if(!new File(dest+"4-6/").exists()){
            new File(dest+"4-6/").mkdir();
        }
        for (int i=1;i<4;i++){
            int pathIndex=i+3,jBeginIndex=8*(int)Math.pow(2,i-1),jEndIndex=11*(int)Math.pow(2,i-1),kBeginIndex=1,kEndIndex=6*((int)Math.pow(2,i-1)*(int)Math.pow(2,i-1));
            //先创建级别目录
            if(!new File(dest+"4-6/"+pathIndex).exists()){
                new File(dest+"4-6/"+pathIndex).mkdir();
            }
            //再创建级别子目录
            for (int j=jBeginIndex;j<jEndIndex;j++){
                if(!new File(dest+"4-6/"+pathIndex+"/"+j).exists()){
                    new File(dest+"4-6/"+pathIndex+"/"+j).mkdir();
                }
            }
            //最后移动图片
            int index=1;
            for (int col=7*(int)Math.pow(2,i-1);col<9*(int)Math.pow(2,i-1);col++){
                for (int row=jBeginIndex;row<jEndIndex;row++){
                    fu.moveFile(dest+levelsImages[i-1]+index+".png",dest+"4-6/"+pathIndex+"/"+row+"/"+col+".png");
                    index+=1;
                }
            }
        }
        // 删除临时目录
        FileUtil.deleteFolder(dest+"temp");
    }
    /**
     * 地图切片生成
     * @param src 为源文件目录
     * @param dest 为切分后保存目录
     * @return
     */
    public synchronized static boolean makeMapPicFiles(String src,String dest)throws Exception {
        File file = new File(src);
        if (file.exists()){
            FileInputStream fis = new FileInputStream(file);
            BufferedImage image = ImageIO.read(fis);
            if(image.getWidth()>=3072 && image.getHeight()>=2048 && ((double)image.getWidth()/(double)image.getHeight())==1.5){
                //1、创建临时目录
                if(!new File(dest+"temp").exists()){
                    new File(dest+"temp").mkdir();
                }
                //2、先将图片缩放到6、5、4级地图切片之和(3072*2048,1536*1024,768*512)
                zoomImage(src,dest+"temp/3072-2048.png",3072,2048);
                zoomImage(src,dest+"temp/1536-1024.png",1536,1024);
                zoomImage(src,dest+"temp/768-512.png",768,512);
                //3、将各级别尺寸的图片进行切分
                cut456LevelImage(src,dest);
                //4、保存到对应路径
                save456LevelImage(src,dest);

                return true;
            }
        }
        return false;
    }



    public static void main(String[] args)throws Exception {
        //zoomImage("C:/Users/knave/Desktop/test.png","C:/Users/knave/Desktop/res.png",600,400);
        //cutImage("C:/Users/knave/Desktop/test.png","C:/Users/knave/Desktop/res/",2,3);
        makeMapPicFiles("C:/Users/knave/Desktop/test.png","C:/Users/knave/Desktop/res/");

    }
}
